package controller.link.game;

import view.client.Console;
import model.Hearer;
import view.client.PanelGame;
import java.io.Serializable;
import java.util.ArrayList;
import controller.Link;
import model.game.Map;
import model.game.NPC;
import model.game.Place;
import model.game.Player;
import model.game.Scenario;
import model.User;
import model.Attendant;
import model.Server;
import model.game.Location;

public class Ask4PlayersRoom implements Link, Serializable {

    private ArrayList<Player> players = new ArrayList<Player>();

    //when a client ask for players, dont need nothing
    public Ask4PlayersRoom() {
    }

    public Ask4PlayersRoom(Hearer h) {
        h.send((Link) this);
    }

    //then a sv receives it and apply their data
    @Override
    public void serverUse(Server server, Attendant at) {
        User user = at.getUser();
        Map map = server.getWorld().getMap();
        Location o = map.whereIsPlayer(user.getPlayer().getName());
        players = new ArrayList<Player>();
        for (int i=0; i<o.getPerson().size(); i++){
            if (o.getPerson().get(i)instanceof NPC){
                players.add(o.getPerson().get(i));
            }else if (!o.getPerson().get(i).equals(user.getPlayer())){
                for (int j=0; j<server.getUsers().size(); j++){
                    if (server.getUsers().get(j).getPlayer().equals(o.getPerson().get(i))){
                        players.add(o.getPerson().get(i));
                    }
                }
            }
        }
        at.send((Link) this);
    }

    @Override
    public void clientUse(Console console) {
        PanelGame pg = console.getPg();
        pg.getModelPeople().removeAllElements();
        for (int i = 0; i < players.size(); i++) {
            pg.getModelPeople().add(i, players.get(i).getName());
        }
        pg.getListAction().revalidate();
        pg.getListAction().repaint();
        pg.getListPlace().revalidate();
        pg.getListPlace().repaint();
        pg.revalidate();
        console.getFrame().repaint();
    }
}
